<?php

$totalCount = 0;
class importRunTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, '', 'frontend'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      new sfCommandOption('file', null, sfCommandOption::PARAMETER_REQUIRED, 'LUA File to convert', 'data/RPBot.lua'),
      // add your own options here
    ));

    $this->namespace        = 'raidpoints';
    $this->name             = 'importRun';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [import|INFO] task does things.
Call it with:

  [php symfony import|INFO]
EOF;
  }

  public function doLogSection($section, $message, $size = NULL, $style ='INFO')
  {
      global $totalCount;

      $q = Doctrine::getTable("DataImportStatus")
          ->createQuery()
          ->orderBy("date desc")
          ->limit(1);
      $r = $q->execute();

      $is = new DataImportStatus();
      $is->setDate(gmdate("Y-m-d H:i:s", time()));
      $is->setStatus($section);
      $is->setText($message);
      $is->setStatustotal($totalCount);
      if (isset($size))
      {
          $is->setStatusbar(0);
          $size = null;
      }
      elseif ($r->count() == 1)
      {
          $is->setStatusbar($r->getFirst()->getStatusbar()+1);
      }
      else
      {
          $is->setStatusbar(0);
      }
      $is->save();
      $this->logsection($section, $message, $size, $style);
  }

  protected function execute($arguments = array(), $options = array())
  {
      $startTime = time();
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
    // add your code here
    //
    $is = new DataImportStatus();
    $is->setDate(gmdate("Y-m-d H:i:s", time()));
    $is->setStatus("Working");
    $is->setText("Starting service...");
    $is->setStatustotal(0);
	$bg = new BackgroundIt();
    $query = Doctrine::getTable("DataImport")
        ->createQuery()
        ->select('SUM(size)');
    $result = $query->execute();
    if ($result->count() > 0)
    {
        $temp = $result->getFirst()->toArray();
        $totalCount = $temp['SUM'];
    }
    
    $this->doLogSection("Working", "Loading temporary cache...", 0);

    $query = Doctrine::getTable("DataImport")
        ->createQuery()
        ->select('text')
        ->limit(1);
    $result = $query->execute();
    while ($result->count() > 0)
    {
        $bg->sys(sfConfig::get('sf_root_dir')."/symfony raidpoints:importReason", sfConfig::get("app_path_to_php"));

        if ($startTime+100 < time())
        {
            $bg->run(sfConfig::get('sf_root_dir').'/symfony raidpoints:importRun', sfConfig::get("app_path_to_php"));
            exit();
        }

        $result = $query->execute();
    }
    //Doctrine::getTable("GuildHistory")->refresh();
    $this->doLogSection("Finished", "Updating points and attendance....");
	Doctrine::getTable("PlayerHistory")->updatePoints();
    $this->doLogSection("Finished", "Finished data import!");
    sleep(2);
    $is = new DataImportStatus();
    $is->setDate(gmdate("Y-m-d H:i:s", time()));
    $is->setStatus("Ready");
    $is->setText("Ready for next import.");
    $is->setStatustotal(0);
    $is->setStatusbar(0);
    $is->save();
    //system(sfConfig::get('sf_root_dir')."/symfony raidpoints:updatepoints");

    /*
    $r = Doctrine::getTable("History")->getLastLog('2009-07-05 17:01:59', 10, 1);
    $r = $r->getFirst();

    $r = Doctrine::getTable("PlayerHistory")->updatePoints(2, 1);

    $newHistory = new History();
    $newHistory->setDate('2009-07-05 17:01:59');
    $newHistory->setActiondate('2009-07-05 17:02:51');
    $newHistory->setSavedate($newHistory->getActiondate());
    $newHistory->setLogdate(time());
    $newHistory->setLogcomment(' ');
    $newHistory->setComment(' ');
    $newHistory->setItemid(0);
    $newHistory->setOfficerid(0);
    $newHistory->setPlayerid(10);
    $newHistory->setRaidid(1);
    $newHistory->setReasonid(831);
    $newHistory->setTtype('I');
    $newHistory->setValue('0');
    $newHistory->setWaitlistflag(0);
    $newHistory->setZoneid(0);
    $newHistory->setAaction("D");
    $newHistory->save();

    $this->logsection("do-nothing", "I did nothing...");
    //print_r($r->getAction());
    */


  }
}
